<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
    >
      <el-form-item label="参数名" prop="paramKey">
        <el-input v-model="dataForm.paramKey" placeholder="参数名"></el-input>
      </el-form-item>
      <el-form-item label="标题名:" prop="paramValue" style="display: flex">
        <template style="display: flex">
          <el-input v-model="dataForm.valueList[0].key" style="width: 200px;"></el-input>
          <el-input v-model="dataForm.valueList[0].value" style="width: 200px;margin-left: 20px"></el-input>
          <span
            @click="addSingerOption(dataForm.valueList.length)"
            class="el-icon-plus"
            style="border: 1px solid #d6d6d6;padding: 10px;border-radius: 4px"
          ></span>
          <div
            v-for="(item,index) in dataForm.valueList"
            :key="index"
            v-show="index>=1"
            style="margin-top: 10px;display: flex"
          >
            <!--            <el-input v-model="item.value" style="width: 120px"></el-input>-->
            <el-input style="width: 200px" v-model="item.key"></el-input>
            <el-input style="width: 200px;margin-left: 20px" v-model="item.value"></el-input>
            <span
              @click="removeOption(index)"
              class="el-icon-delete"
              style="border: 1px solid #d6d6d6;padding: 10px;border-radius: 4px"
            ></span>
          </div>
        </template>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :loading="loading">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: '',
        paramKey: '',
        paramValue: [],
        valueList: [
          {
            key: '',
            value: ''
          }
        ]
      },
      loading: false,
      dataRule: {
        name: [
          { required: true, message: '规范名称不能为空', trigger: 'blur' }
        ],
        //          propertyValue: [
        //            { required: true, message: '属性值不能为空', trigger: 'blur' }
        //          ],
        sortOrder: [
          { required: true, message: '排序不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        this.dataForm.valueList = [
          {
            key: '',
            value: ''
          }
        ]
        if (this.dataForm.id) {
          this.$http({
            url: `/config/info/${this.dataForm.id}`,
            method: 'post',
            data: { id: this.dataForm.id }
          }).then(data => {
            console.log(data)
            if (data && data.code === 0) {
            }
          })
        }
      })
    },
    // 增加一个选项
    addSingerOption(index) {
      if (index === 26) {
        this.$message.error('最多只能有26个选项')
        return false
      }
      let option = {
        key: index,
        value: ''
      }
      this.dataForm.valueList.push(option)
    },
    // 删除一个选项
    removeOption(index) {
      this.dataForm.valueList.splice(index, 1)
    },
    // 表单提交
    dataFormSubmit() {
      this.dataForm.paramValue = JSON.stringify(this.dataForm.valueList)
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.$http({
            url: `/tenantsysconfig/${
              !this.dataForm.id ? 'save' : 'update'
            }`,
            method: 'post',
            data: {
              paramKey: this.dataForm.paramKey,
              paramValue: this.dataForm.paramValue
            }
          }).then(data => {
            console.log(data)
            if (data && data.code === 200) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500
              })
              this.visible = false
              this.$emit('refreshDataList')
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
